home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / mewin10s.zip / INPUT.C < prev    next >
C/C++ Source or Header  |  1992-03-14  |  23KB  |  978 lines

  1. /*    Input:    Various input routines for MicroEMACS
  2.         written by Daniel Lawrence
  3.         5/9/86                        */
  4.  
  5. /*
  6.     Notes:
  7.  
  8.     MicroEMACS's kernel processes two distinct forms of
  9.     characters.  One of these is a standard unsigned character
  10.     which is used in the edited text.  The other form, called
  11.     an EMACS Extended Character is a 2 byte value which contains
  12.     both an ascii value, and flags for certain prefixes/events.
  13.  
  14.     Bit    Usage
  15.     ---    -----
  16.     0 -> 7    Standard 8 bit ascii character
  17.     8    Control key flag
  18.     9    META prefix flag
  19.     10    ^X prefix flag
  20.     11    Function key flag
  21.     12    Mouse prefix
  22.     13    Shifted flag (not needed on alpha shifted characters)
  23.     14    Alterate prefix (ALT key on PCs)
  24.  
  25.     The machine dependent driver is responsible for returning
  26.     a byte stream from the various input devices with various
  27.     prefixes/events embedded as escape codes.  Zero is used as the
  28.     value indicating an escape sequence is next.  The format of
  29.     an escape sequence is as follows:
  30.  
  31.     0        Escape indicator
  32.     <prefix byte>    upper byte of extended character
  33.     {<col><row>}    col, row position if the prefix byte
  34.             indicated a mouse event or a menu selection
  35.             in which case these form a 16 bit menu ID
  36.     <event code>    value of event
  37.  
  38.     Two successive zeroes are used to indicate an actual
  39.     null being input.  These values are then interpreted by
  40.     getkey() to construct the proper extended character
  41.     sequences to pass to the MicroEMACS kernel.
  42. */
  43.  
  44. #include    <stdio.h>
  45. #include    "estruct.h"
  46. #include    "eproto.h"
  47. #include    "edef.h"
  48. #include    "elang.h"
  49.  
  50. /*
  51.  * Ask a yes or no question in the message line. Return either TRUE, FALSE, or
  52.  * ABORT. The ABORT status is returned if the user bumps out of the question
  53.  * with a ^G. Used any time a confirmation is required.
  54.  */
  55.  
  56. #if     !WINDOW_MSWIN   /* for MS Windows, mlyesno is defined in mswsys.c */
  57. PASCAL NEAR mlyesno(prompt)
  58.  
  59. char *prompt;
  60.  
  61. {
  62.     int  c;            /* input character */
  63.     char buf[NPAT];        /* prompt to user */
  64.  
  65.     for (;;) {
  66.         /* build and prompt the user */
  67.         strcpy(buf, prompt);
  68.         strcat(buf, TEXT162);
  69. /*                          " [y/n]? " */
  70.         mlwrite(buf);
  71.  
  72.         /* get the response */
  73.             c = getcmd();   /* getcmd() lets us check for anything that might */
  74.                             /* generate a 'y' or 'Y' in case use screws up */
  75.  
  76.         if (c == ectoc(abortc))        /* Bail out! */
  77.             return(ABORT);
  78.  
  79.             if  ((c == 'n') || (c == 'N')
  80.                 || (c & (SPEC|ALTD|CTRL|META|CTLX|MOUS)))
  81.                     return(FALSE);  /* ONLY 'y' or 'Y' allowed!!! */
  82.  
  83. #if    FRENCH
  84.         if (c=='o' || c=='O')
  85.             return(TRUE);
  86. #endif
  87.  
  88.         if (c=='y' || c=='Y')
  89.             return(TRUE);
  90.  
  91.         return(FALSE);
  92.     }
  93. }
  94. #endif
  95.  
  96. /*
  97.  * Write a prompt into the message line, then read back a response. Keep
  98.  * track of the physical position of the cursor. If we are in a keyboard
  99.  * macro throw the prompt away, and return the remembered response. This
  100.  * lets macros run at full speed. The reply is always terminated by a carriage
  101.  * return. Handle erase, kill, and abort keys.
  102.  */
  103.  
  104. PASCAL NEAR mlreply(prompt, buf, nbuf)
  105.  
  106. char *prompt;
  107. char *buf;
  108. int nbuf;
  109.  
  110. {
  111.     return(nextarg(prompt, buf, nbuf, ctoec((int) '\r')));
  112. }
  113.  
  114. PASCAL NEAR mltreply(prompt, buf, nbuf, eolchar)
  115.  
  116. char *prompt;
  117. char *buf;
  118. int nbuf;
  119. int eolchar;
  120.  
  121. {
  122.     return(nextarg(prompt, buf, nbuf, eolchar));
  123. }
  124.  
  125. /*    ectoc:    expanded character to character
  126.         collapse the CTRL and SPEC flags back into an ascii code   */
  127.  
  128. PASCAL NEAR ectoc(c)
  129.  
  130. int c;
  131.  
  132. {
  133.     if (c & CTRL)
  134.         c = c ^ (CTRL | 0x40);
  135.     if (c & SPEC)
  136.         c= c & 255;
  137.     return(c);
  138. }
  139.  
  140. /*    ctoec:    character to extended character
  141.         pull out the CTRL and SPEC prefixes (if possible)    */
  142.  
  143. PASCAL NEAR ctoec(c)
  144.  
  145. int c;
  146.  
  147. {
  148.         if ((c>=0x00 && c<=0x1F) || c == 0x7F)
  149.                 c = CTRL | (c ^ 0x40);
  150.         return(c);
  151. }
  152.  
  153. /* get a command name from the command line. Command completion means
  154.    that pressing a <SPACE> will attempt to complete an unfinished command
  155.    name if it is unique.
  156. */
  157.  
  158. #if    MSC
  159. int (PASCAL NEAR *PASCAL NEAR getname(char *prompt))(void)
  160. #else
  161. int (PASCAL NEAR *PASCAL NEAR getname(prompt))()
  162.  
  163. char *prompt;    /* string to prompt with */
  164. #endif
  165.  
  166. {
  167.     char *sp;    /* ptr to the returned string */
  168.  
  169.     sp = complete(prompt, NULL, CMP_COMMAND, NSTRING);
  170.     if (sp == NULL)
  171.         return(NULL);
  172.  
  173.     return(fncmatch(sp));
  174. }
  175.  
  176. /*    getcbuf:    get a completion from the user for a buffer name.
  177.  
  178.             I was goaded into this by lots of other people's
  179.             completion code.
  180. */
  181.  
  182. BUFFER *PASCAL NEAR getcbuf(prompt, defval, createflag)
  183.  
  184. char *prompt;        /* prompt to user on command line */
  185. char *defval;        /* default value to display to user */
  186. int createflag;        /* should this create a new buffer? */
  187.  
  188. {
  189.     char *sp;    /* ptr to the returned string */
  190.  
  191.     sp = complete(prompt, defval, CMP_BUFFER, NBUFN);
  192.     if (sp == NULL)
  193.         return(NULL);
  194.  
  195.     return(bfind(sp, createflag, 0));
  196. }
  197.  
  198. char *PASCAL NEAR gtfilename(prompt)
  199.  
  200. char *prompt;        /* prompt to user on command line */
  201.  
  202. {
  203. #if     WINDOW_MSWIN
  204.         static char  sp [NFILEN];
  205.  
  206.         if (!FILENAMEREPLY (prompt, sp, NFILEN)) return NULL;
  207. #else
  208.     char *sp;    /* ptr to the returned string */
  209.  
  210.     sp = complete(prompt, NULL, CMP_FILENAME, NFILEN);
  211.     if (sp == NULL)
  212.         return(NULL);
  213. #endif
  214.     return(sp);
  215. }
  216.  
  217. char *PASCAL NEAR complete(prompt, defval, type, maxlen)
  218.  
  219. char *prompt;        /* prompt to user on command line */
  220. char *defval;        /* default value to display to user */
  221. int type;        /* type of what we are completing */
  222. int maxlen;        /* maximum length of input field */
  223.  
  224. {
  225.     register int c;        /* current input character */
  226.     register int ec;    /* extended input character */
  227.     int cpos;        /* current column on screen output */
  228.     static char buf[NSTRING];/* buffer to hold tentative name */
  229.  
  230.     /* if we are executing a command line get the next arg and match it */
  231.     if (clexec) {
  232.         if (macarg(buf) != TRUE)
  233.             return(NULL);
  234.         return(buf);
  235.     }
  236.  
  237.     /* starting at the beginning of the string buffer */
  238.     cpos = 0;
  239.  
  240.     /* if it exists, prompt the user for a buffer name */
  241.     if (prompt)
  242.         if (type == CMP_COMMAND)
  243.             mlwrite("%s", prompt);
  244.         else if (defval)
  245.             mlwrite("%s[%s]: ", prompt, defval);
  246.         else
  247.             mlwrite("%s: ", prompt);
  248.  
  249.     /* build a name string from the keyboard */
  250.     while (TRUE) {
  251.  
  252.         /* get the keystroke and decode it */
  253.         ec = getkey();
  254.         c = ectoc(ec);        
  255.  
  256.         /* if we are at the end, just match it */
  257.         if (c == '\n'  ||  c == '\r') {
  258.             if (defval && cpos==0)
  259.                 return(defval);
  260.             else {
  261.                 buf[cpos] = 0;
  262.                 return(buf);
  263.             }
  264.  
  265.         } else if (ec == abortc) {    /* Bell, abort */
  266.             ctrlg(FALSE, 0);
  267.             TTflush();
  268.             return(NULL);
  269.  
  270.         } else if (c == 0x7F || c == 0x08) {    /* rubout/erase */
  271.             if (cpos != 0) {
  272.                 mlout('\b');
  273.                 mlout(' ');
  274.                 mlout('\b');
  275.                 --ttcol;
  276.                 --cpos;
  277.                 TTflush();
  278.             }
  279.  
  280.         } else if (c == 0x15) {    /* C-U, kill */
  281.             while (cpos != 0) {
  282.                 mlout('\b');
  283.                 mlout(' ');
  284.                 mlout('\b');
  285.                 --cpos;
  286.                 --ttcol;
  287.             }
  288.             TTflush();
  289.  
  290.         } else if ((c == ' ') || (ec == sterm) || (c == '\t')) {    
  291.             /* attempt a completion */
  292.             switch (type) {
  293.                 case CMP_BUFFER:
  294.                     comp_buffer(buf, &cpos);
  295.                     break;
  296.                 case CMP_COMMAND:
  297.                     comp_command(buf, &cpos);
  298.                     break;
  299. #if     !WINDOW_MSWIN
  300.                 case CMP_FILENAME:
  301.                     comp_file(buf, &cpos);
  302.                     break;
  303. #endif
  304.             }
  305.  
  306.             TTflush();
  307.             if (buf[cpos - 1] == 0)
  308.                 return(buf);
  309.             goto clist;
  310.  
  311.         } else if (c == '?') {    
  312.  
  313. clist:            /* make a completion list! */
  314.             switch (type) {
  315.                 case CMP_BUFFER:
  316.                     clist_buffer(buf, &cpos);
  317.                     break;
  318.                 case CMP_COMMAND:
  319.                     clist_command(buf, &cpos);
  320.                     break;
  321. #if     !WINDOW_MSWIN
  322.                 case CMP_FILENAME:
  323.                     clist_file(buf, &cpos);
  324.                     break;
  325. #endif
  326.             }
  327.             update(TRUE);
  328.  
  329.             /* if it exists, reprompt the user */
  330.             if (prompt) {
  331.                 buf[cpos] = 0;
  332.                 if (type == CMP_COMMAND)
  333.                     mlwrite("%s%s", prompt, buf);
  334.                 else if (defval)
  335.                     mlwrite("%s[%s]: %s", prompt, defval, buf);
  336.                 else
  337.                     mlwrite("%s: %s", prompt, buf);
  338.             }
  339.  
  340.         } else {
  341.             if (cpos < maxlen && c > ' ') {
  342.                 buf[cpos++] = c;
  343.                 mlout(c);
  344.                 ++ttcol;
  345.                 TTflush();
  346.             }
  347.         }
  348.     }
  349. }
  350.  
  351. /*    comp_command:    Attempt a completion on a command name    */
  352.  
  353. comp_command(name, cpos)
  354.  
  355. char *name;    /* command containing the current name to complete */
  356. int *cpos;    /* ptr to position of next character to insert */
  357.  
  358. {
  359.     register NBIND *bp;    /* trial command to complete */
  360.     register int index;    /* index into strings to compare */
  361.     register int curbind;    /* index into the names[] array */
  362.     register NBIND *match;    /* last command that matches string */
  363.     register int matchflag;    /* did this command name match? */
  364.     register int comflag;    /* was there a completion at all? */
  365.  
  366.     /* start attempting completions, one character at a time */
  367.     comflag = FALSE;
  368.     curbind = 0;
  369.     while (*cpos < NSTRING) {
  370.  
  371.         /* first, we start at the first command and scan the list */
  372.         match = NULL;
  373.         curbind = 0;
  374.         while (curbind <= numfunc) {
  375.  
  376.             /* is this a match? */
  377.             bp = &names[curbind];
  378.             matchflag = TRUE;
  379.             for (index = 0; index < *cpos; index++)
  380.                 if (name[index] != bp->n_name[index]) {
  381.                     matchflag = FALSE;
  382.                     break;
  383.                 }
  384.  
  385.             /* if it is a match */
  386.             if (matchflag) {
  387.  
  388.                 /* if this is the first match, simply record it */
  389.                 if (match == NULL) {
  390.                     match = bp;
  391.                     name[*cpos] = bp->n_name[*cpos];
  392.                 } else {
  393.                     /* if there's a difference, stop here */
  394.                     if (name[*cpos] != bp->n_name[*cpos])
  395.                         return;
  396.                 }
  397.             }
  398.  
  399.             /* on to the next command */
  400.             curbind++;
  401.         }
  402.  
  403.         /* with no match, we are done */
  404.         if (match == NULL) {
  405.             /* beep if we never matched */
  406.             if (comflag == FALSE)
  407.                 TTbeep();
  408.             return;
  409.         }
  410.  
  411.         /* if we have completed all the way... go back */
  412.         if (name[*cpos] == 0) {
  413.             (*cpos)++;
  414.             return;
  415.         }
  416.  
  417.         /* remember we matched, and complete one character */
  418.         comflag = TRUE;
  419.         TTputc(name[(*cpos)++]);
  420.         TTflush();
  421.     }
  422.  
  423.     /* don't allow a completion past the end of the max command name length */
  424.     return;
  425. }
  426.  
  427. /*    clist_command:    Make a completion list based on a partial name */
  428.  
  429. clist_command(name, cpos)
  430.  
  431. char *name;    /* command containing the current name to complete */
  432. int *cpos;    /* ptr to position of next character to insert */
  433.  
  434. {
  435.     register NBIND *bp;    /* trial command to complete */
  436.     register int curbind;    /* index into the names[] array */
  437.     register int name_len;    /* current length of input string */
  438.     register BUFFER *listbuf;/* buffer to put completion list into */
  439.  
  440.     /* get a buffer for the completion list */
  441.     listbuf = bfind("[Completion list]", TRUE, BFINVS);
  442.     if (listbuf == NULL || bclear(listbuf) == FALSE) {
  443.         ctrlg(FALSE, 0);
  444.         TTflush();
  445.         return;
  446.     }
  447.  
  448.     name_len = *cpos;
  449.  
  450.     /* first, we start at the first command and scan the list */
  451.     for (curbind = 0; curbind <= numfunc; curbind++) {
  452.  
  453.         /* is this a match? */
  454.         bp = &names[curbind];
  455.         if (strncmp(name, bp->n_name, name_len) == 0)
  456.             addline(listbuf, bp->n_name);
  457.     }
  458.  
  459.     wpopup(listbuf);
  460.     return;
  461. }
  462.  
  463. /*    comp_buffer:    Attempt a completion on a buffer name    */
  464.  
  465. comp_buffer(name, cpos)
  466.  
  467. char *name;    /* buffer containing the current name to complete */
  468. int *cpos;    /* ptr to position of next character to insert */
  469.  
  470. {
  471.     register BUFFER *bp;    /* trial buffer to complete */
  472.     register int index;    /* index into strings to compare */
  473.     register BUFFER *match;    /* last buffer that matches string */
  474.     register int matchflag;    /* did this buffer name match? */
  475.     register int comflag;    /* was there a completion at all? */
  476.  
  477.     /* start attempting completions, one character at a time */
  478.     comflag = FALSE;
  479.     while (*cpos < NBUFN) {
  480.  
  481.         /* first, we start at the first buffer and scan the list */
  482.         match = NULL;
  483.         bp = bheadp;
  484.         while (bp) {
  485.  
  486.             /* is this a match? */
  487.             matchflag = TRUE;
  488.             for (index = 0; index < *cpos; index++)
  489.                 if (name[index] != bp->b_bname[index]) {
  490.                     matchflag = FALSE;
  491.                     break;
  492.                 }
  493.  
  494.             /* if it is a match */
  495.             if (matchflag) {
  496.  
  497.                 /* if this is the first match, simply record it */
  498.                 if (match == NULL) {
  499.                     match = bp;
  500.                     name[*cpos] = bp->b_bname[*cpos];
  501.                 } else {
  502.                     /* if there's a difference, stop here */
  503.                     if (name[*cpos] != bp->b_bname[*cpos])
  504.                         return;
  505.                 }
  506.             }
  507.  
  508.             /* on to the next buffer */
  509.             bp = bp->b_bufp;
  510.         }
  511.  
  512.         /* with no match, we are done */
  513.         if (match == NULL) {
  514.             /* beep if we never matched */
  515.             if (comflag == FALSE)
  516.                 TTbeep();
  517.             return;
  518.         }
  519.  
  520.         /* if we have completed all the way... go back */
  521.         if (name[*cpos] == 0) {
  522.             (*cpos)++;
  523.             return;
  524.         }
  525.  
  526.         /* remember we matched, and complete one character */
  527.         comflag = TRUE;
  528.         TTputc(name[(*cpos)++]);
  529.         TTflush();
  530.     }
  531.  
  532.     /* don't allow a completion past the end of the max buffer name length */
  533.     return;
  534. }
  535.  
  536. /*    clist_buffer:    Make a completion list based on a partial buffer name */
  537.  
  538. clist_buffer(name, cpos)
  539.  
  540. char *name;    /* command containing the current name to complete */
  541. int *cpos;    /* ptr to position of next character to insert */
  542.  
  543. {
  544.     register int name_len;    /* current length of input string */
  545.     register BUFFER *listbuf;/* buffer to put completion list into */
  546.     register BUFFER *bp;    /* trial buffer to complete */
  547.  
  548.     /* get a buffer for the completion list */
  549.     listbuf = bfind("[Completion list]", TRUE, BFINVS);
  550.     if (listbuf == NULL || bclear(listbuf) == FALSE) {
  551.         ctrlg(FALSE, 0);
  552.         TTflush();
  553.         return;
  554.     }
  555.  
  556.     /* first, we start at the first buffer and scan the list */
  557.     name_len = *cpos;
  558.     bp = bheadp;
  559.  
  560.     while (bp) {
  561.  
  562.         /* is this a match? */
  563.         if (strncmp(name, bp->b_bname, name_len) == 0)
  564.             addline(listbuf, bp->b_bname);
  565.  
  566.         /* on to the next buffer */
  567.         bp = bp->b_bufp;
  568.     }
  569.  
  570.     wpopup(listbuf);
  571.     return;
  572. }
  573.  
  574. #if     !WINDOW_MSWIN
  575. /*    comp_file:    Attempt a completion on a file name    */
  576.  
  577. comp_file(name, cpos)
  578.  
  579. char *name;    /* file containing the current name to complete */
  580. int *cpos;    /* ptr to position of next character to insert */
  581.  
  582. {
  583.     register char *fname;    /* trial file to complete */
  584.     register int index;    /* index into strings to compare */
  585.  
  586.     register int matches;    /* number of matches for name */
  587.     char longestmatch[NSTRING]; /* temp buffer for longest match */
  588.     int longestlen;     /* length of longest match (always > *cpos) */
  589.  
  590.     /* everything (or nothing) matches an empty string */
  591.     if (*cpos == 0)
  592.         return;
  593.  
  594.     /* first, we start at the first file and scan the list */
  595.     matches = 0;
  596.     name[*cpos] = 0;
  597.     fname = getffile(name);
  598.     while (fname) {
  599.  
  600.         /* is this a match? */
  601.         if (strncmp(name,fname,*cpos) == 0) {
  602.  
  603.             /* count the number of matches */
  604.             matches++;
  605.  
  606.             /* if this is the first match, simply record it */
  607.             if (matches == 1) {
  608.                 strcpy(longestmatch,fname);
  609.                 longestlen = strlen(longestmatch);
  610.             } else {
  611.  
  612.                 /* if there's a difference, stop here */
  613.                 if (longestmatch[*cpos] != fname[*cpos])
  614.                     return;
  615.  
  616.                 for (index = (*cpos) + 1; index < longestlen; index++)
  617.                     if (longestmatch[index] != fname[index]) {
  618.                         longestlen = index;
  619.                         longestmatch[longestlen] = 0;
  620.                     }
  621.             }
  622.         }
  623.  
  624.         /* on to the next file */
  625.         fname = getnfile();
  626.     }
  627.  
  628.     /* beep if we never matched */
  629.     if (matches == 0) {
  630.         TTbeep();
  631.         return;
  632.     }
  633.  
  634.     /* the longestmatch array contains the longest match so copy and print it */
  635.     for ( ; (*cpos < (NSTRING-1)) && (*cpos < longestlen); (*cpos)++) {
  636.         name[*cpos] = longestmatch[*cpos];
  637.         TTputc(name[*cpos]);
  638.     }
  639.  
  640.     name[*cpos] = 0;
  641.  
  642.     /* if only one file matched then increment cpos to signal complete() */
  643.     /* that this was a complete match.  If a directory was matched then */
  644.     /* last character will be the DIRSEPCHAR.  In this case we do NOT *
  645.     /* want to signal a complete match. */
  646.     if ((matches == 1) && (name[(*cpos)-1] != DIRSEPCHAR))
  647.         (*cpos)++;
  648.  
  649.     TTflush();
  650.  
  651.     return;
  652. }
  653.  
  654. /*    clist_file:    Make a completion list based on a partial file name */
  655.  
  656. clist_file(name, cpos)
  657.  
  658. char *name;    /* command containing the current name to complete */
  659. int *cpos;    /* ptr to position of next character to insert */
  660.  
  661. {
  662.     register int name_len;    /* current length of input string */
  663.     register BUFFER *listbuf;/* buffer to put completion list into */
  664.     register char *fname;    /* trial file to complete */
  665.  
  666.     /* get a buffer for the completion list */
  667.     listbuf = bfind("[Completion list]", TRUE, BFINVS);
  668.     if (listbuf == NULL || bclear(listbuf) == FALSE) {
  669.         ctrlg(FALSE, 0);
  670.         TTflush();
  671.         return;
  672.     }
  673.  
  674.     /* first, we start at the first file and scan the list */
  675.     name_len = *cpos;
  676.     name[*cpos] = 0;
  677.     fname = getffile(name);
  678.  
  679.     /* first, we start at the first file and scan the list */
  680.     while (fname) {
  681.  
  682.         /* is this a match? */
  683.         if (strncmp(name, fname, name_len) == 0)
  684.             addline(listbuf, fname);
  685.  
  686.         /* on to the next file */
  687.         fname = getnfile();
  688.     }
  689.  
  690.     wpopup(listbuf);
  691.     return;
  692. }
  693. #endif
  694.  
  695. /*    tgetc:    Get a key from the terminal driver, resolve any keyboard
  696.         macro action                    */
  697.  
  698. int PASCAL NEAR tgetc()
  699.  
  700. {
  701.     int c;    /* fetched character */
  702.  
  703.     /* if we are playing a keyboard macro back, */
  704.     if (kbdmode == PLAY) {
  705.  
  706.         /* if there is some left... */
  707.         if (kbdptr < kbdend)
  708.             return((int)*kbdptr++);
  709.  
  710.         /* at the end of last repitition? */
  711.         if (--kbdrep < 1) {
  712.             kbdmode = STOP;
  713. #if    VISMAC == 0
  714.             /* force a screen update after all is done */
  715.             update(FALSE);
  716. #endif
  717.         } else {
  718.  
  719.             /* reset the macro to the begining for the next rep */
  720.             kbdptr = &kbdm[0];
  721.             return((int)*kbdptr++);
  722.         }
  723.     }
  724.  
  725.     /* if no pending character */
  726.     if (cpending == FALSE) {
  727.  
  728.         /* fetch a character from the terminal driver */
  729.         c = TTgetc();
  730.  
  731.     } else {
  732.         
  733.         c = charpending;
  734.         cpending = FALSE;
  735.     }
  736.  
  737.     /* record it for $lastkey */
  738.     lastkey = c;
  739.  
  740.     /* save it if we need to */
  741.     if (kbdmode == RECORD) {
  742.         *kbdptr++ = c;
  743.         kbdend = kbdptr;
  744.  
  745.         /* don't overrun the buffer */
  746.         if (kbdptr == &kbdm[NKBDM - 1]) {
  747.             kbdmode = STOP;
  748.             TTbeep();
  749.         }
  750.     }
  751.  
  752.     /* and finally give the char back */
  753.     return(c);
  754. }
  755.  
  756. /*    getkey:    Get one keystroke. The legal prefixs here
  757.             are the SPEC, MOUS and CTRL prefixes.
  758. */
  759.  
  760. PASCAL NEAR getkey()
  761.  
  762. {
  763.     int c;        /* next input character */
  764.     int upper;    /* upper byte of the extended sequence */
  765.  
  766.     /* get a keystroke */
  767.         c = tgetc();
  768.  
  769.     /* if it exists, process an escape sequence */
  770.     if (c == 0) {
  771.  
  772.         /* get the event type */
  773.         upper = tgetc();
  774.  
  775.         /* mouse events need us to read in the row/col */
  776.         if (upper & (MOUS >> 8)) {
  777.             /* grab the x/y position of the mouse */
  778.             xpos = tgetc();
  779.             ypos = tgetc();
  780.         }
  781.  
  782.         /* get the event code */
  783.         c = tgetc();
  784.  
  785.         /* if it is a function key... map it */
  786.         c = (upper << 8) | c;
  787.     }
  788.  
  789.     /* yank out the control prefix */
  790.         if (((c & 255) >=0x00 && (c & 255) <= 0x1F) || (c & 255) == 0x7F)
  791.                 c = CTRL | (c ^ 0x40);
  792.  
  793.     /* return the character */
  794.         return(c);
  795. }
  796.  
  797. /*    GETCMD:    Get a command from the keyboard. Process all applicable
  798.         prefix keys
  799.                             */
  800. PASCAL NEAR getcmd()
  801.  
  802. {
  803.     int c;        /* fetched keystroke */
  804.     KEYTAB *key;    /* ptr to a key entry */
  805.  
  806.     /* get initial character */
  807.     c = getkey();
  808.     key = getbind(c);
  809.  
  810.     /* resolve META and CTLX prefixes */
  811.     if (key) {
  812.         if (key->k_ptr.fp == meta) {
  813.             c = getkey();
  814. #if    SMOS
  815.             c = upperc(c&255) | (c & ~255); /* Force to upper */
  816. #else
  817.             c = upperc(c) | (c & ~255);    /* Force to upper */
  818. #endif
  819.             c |= META;
  820.         } else if (key->k_ptr.fp == cex) {
  821.             c = getkey();
  822. #if    SMOS
  823.             c = upperc(c&255) | (c & ~255); /* Force to upper */
  824. #else
  825.             c = upperc(c) | (c & ~255);    /* Force to upper */
  826. #endif
  827.             c |= CTLX;
  828.         }
  829.     }
  830.  
  831.     /* return it */
  832.     return(c);
  833. }
  834.  
  835. /*    A more generalized prompt/reply function allowing the caller
  836.     to specify the proper terminator. If the terminator is not
  837.     a return('\r'), return will echo as "<NL>"
  838.                             */
  839. PASCAL NEAR getstring(prompt, buf, nbuf, eolchar)
  840.  
  841. char *prompt;
  842. char *buf;
  843. int nbuf;
  844. int eolchar;
  845.  
  846. {
  847.     register int cpos;    /* current character position in string */
  848.     register int c;        /* current input character */
  849.     register int quotef;    /* are we quoting the next char? */
  850.  
  851.     cpos = 0;
  852.     quotef = FALSE;
  853.  
  854.     /* prompt the user for the input string */
  855.     if (discmd)
  856.         mlwrite(prompt);
  857.     else
  858.         movecursor(term.t_nrow, 0);
  859.  
  860.     for (;;) {
  861.         /* get a character from the user */
  862.         c = getkey();
  863.  
  864.         /* if they hit the line terminate, wrap it up */
  865.         if (c == eolchar && quotef == FALSE) {
  866.             buf[cpos++] = 0;
  867.  
  868.             /* clear the message line */
  869.             mlerase();
  870.             TTflush();
  871.  
  872.             /* if we default the buffer, return FALSE */
  873.             if (buf[0] == 0)
  874.                 return(FALSE);
  875.  
  876.             return(TRUE);
  877.         }
  878.  
  879.         /* change from command form back to character form */
  880.         c = ectoc(c);
  881.  
  882.         if (c == ectoc(abortc) && quotef == FALSE) {
  883.             /* Abort the input? */
  884.             ctrlg(FALSE, 0);
  885.             TTflush();
  886.             return(ABORT);
  887.         } else if ((c==0x7F || c==0x08) && quotef==FALSE) {
  888.             /* rubout/erase */
  889.             if (cpos != 0) {
  890.                 outstring("\b \b");
  891.                 --ttcol;
  892.  
  893.                 if (buf[--cpos] < 0x20) {
  894.                     outstring("\b \b");
  895.                     --ttcol;
  896.                 }
  897.  
  898.                 if (buf[cpos] == '\r') {
  899.                     outstring("\b\b  \b\b");
  900.                     ttcol -= 2;
  901.                 }
  902.                 TTflush();
  903.             }
  904.  
  905.          } else if (c == 0x0b && quotef == FALSE) {
  906.  
  907.              /* C-K, kill default buffer and return null */
  908.  
  909.              /* clear the buffer */
  910.              buf[0] = 0;
  911.  
  912.              /* clear the message line and return */
  913.              mlwrite("");
  914.              TTflush();
  915.              return(TRUE);
  916.  
  917.         } else if (c == 0x15 && quotef == FALSE) {
  918.  
  919.             /* C-U, kill */
  920.             while (cpos != 0) {
  921.                 outstring("\b \b");
  922.                 --ttcol;
  923.  
  924.                 if (buf[--cpos] < 0x20) {
  925.                     outstring("\b \b");
  926.                     --ttcol;
  927.                 }
  928.             }
  929.             TTflush();
  930.  
  931.         } else if (c == ectoc(quotec) && quotef == FALSE) {
  932.             quotef = TRUE;
  933.         } else {
  934.             quotef = FALSE;
  935.             if (cpos < nbuf-1) {
  936.                 buf[cpos++] = c;
  937.  
  938.                 if ((c < ' ') && (c != '\r')) {
  939.                     outstring("^");
  940.                     ++ttcol;
  941.                     c ^= 0x40;
  942.                 }
  943.  
  944.                 if (c != '\r') {
  945.                     if (disinp)
  946.                         mlout(c);
  947.                 } else {    /* put out <NL> for <ret> */
  948.                     outstring("<NL>");
  949.                     ttcol += 3;
  950.                 }
  951.                 ++ttcol;
  952.                 TTflush();
  953.             }
  954.         }
  955.     }
  956. }
  957.  
  958. PASCAL NEAR outstring(s) /* output a string of input characters */
  959.  
  960. char *s;    /* string to output */
  961.  
  962. {
  963.     if (disinp)
  964.         while (*s)
  965.             mlout(*s++);
  966. }
  967.  
  968. PASCAL NEAR ostring(s)    /* output a string of output characters */
  969.  
  970. char *s;    /* string to output */
  971.  
  972. {
  973.     if (discmd)
  974.         while (*s)
  975.             mlout(*s++);
  976. }
  977.  
  978.